package cn.tedu.property.management.service.impl;

import cn.tedu.property.management.ex.ServiceException;
import cn.tedu.property.management.mapper.OwnerMapper;
import cn.tedu.property.management.pojo.dto.OwnerAddNewDTO;
import cn.tedu.property.management.pojo.dto.OwnerUpdateDTO;
import cn.tedu.property.management.pojo.entity.Owner;
import cn.tedu.property.management.pojo.vo.OwnerListVO;
import cn.tedu.property.management.pojo.vo.OwnerStandardVO;
import cn.tedu.property.management.service.IOwnerService;
import cn.tedu.property.management.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OwnerServiceImpl implements IOwnerService {
    @Autowired
    private OwnerMapper ownerMapper;
    @Override
    public void insert(OwnerAddNewDTO ownerAddNewDTO) {
        String roomNumber = ownerAddNewDTO.getRoomNumber();
        OwnerStandardVO ownerByRoomNumber = ownerMapper.getOwnerByRoomNumber(roomNumber);
        if(ownerByRoomNumber!=null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"房间号被占用，请检查！");
        }
        Owner owner = new Owner();
        BeanUtils.copyProperties(ownerAddNewDTO,owner);
        int rows = ownerMapper.addNewOwner(owner);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERR_INSERT,"添加业主失败，服务器繁忙，请稍后再试！");
        }
        log.debug("新增成功！参数：{}",owner);
    }

    @Override
    public void deleteById(Long id) {
        OwnerStandardVO ownerById = ownerMapper.getOwnerById(id);
        if(ownerById==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"尝试删除的数据不存在，请重试！");
        }
        int rows = ownerMapper.deleteById(id);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERR_DELETE,"删除失败，服务器繁忙，请稍后再试！");
        }
        log.debug("删除成功！参数：{}",id);
    }

    @Override
    public void updateById(Long id, OwnerUpdateDTO ownerUpdateDTO) {
        Owner owner = new Owner();
        BeanUtils.copyProperties(ownerUpdateDTO,owner);
        owner.setId(id);
        int rows = ownerMapper.updateById(owner);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"更新失败，服务器繁忙，请稍后再试！");
        }
        log.debug("更新成功！参数：{}",owner);
    }

    @Override
    public OwnerStandardVO getOwnerByName(String name) {
        OwnerStandardVO ownerByName = ownerMapper.getOwnerByName(name);
        if(ownerByName==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"未查询到数据！");
        }
        return ownerByName;
    }

    @Override
    public OwnerStandardVO getOwnerByPhone(String phone) {
        OwnerStandardVO ownerByPhone = ownerMapper.getOwnerByPhone(phone);
        if(ownerByPhone==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"未查询到数据！");
        }
        return ownerByPhone;
    }

    @Override
    public OwnerStandardVO getOwnerById(Long id) {
        OwnerStandardVO ownerById = ownerMapper.getOwnerById(id);
        if(ownerById==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"未查询到数据！");
        }
        return ownerById;
    }

    @Override
    public OwnerStandardVO getOwnerByRoomNumber(String roomNumber) {
        OwnerStandardVO ownerByRoomNumber = ownerMapper.getOwnerByRoomNumber(roomNumber);
        if(ownerByRoomNumber==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"未查询到数据！");
        }
        return ownerByRoomNumber;
    }

    @Override
    public List<OwnerListVO> getAllOwner() {
        List<OwnerListVO> allOwner = ownerMapper.getAllOwner();
        if(allOwner==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"未查询到数据！");
        }
        return allOwner;
    }
}
